package com.wlc.controller;

import com.wlc.pojo.Menu;
import com.wlc.pojo.Result;
import com.wlc.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/{id}")
    public Result<Menu> getById(@PathVariable Long id) {
        log.info("根据ID查询菜单: {}", id);
        try {
            Menu menu = menuService.findById(id);
            return Result.success(menu);
        } catch (Exception e) {
            log.error("查询菜单失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<List<Menu>> list() {
        log.info("查询所有菜单列表");
        try {
            List<Menu> menus = menuService.findAll();
            return Result.success(menus);
        } catch (Exception e) {
            log.error("查询菜单列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/tree")
    public Result<List<Menu>> tree() {
        log.info("查询菜单树形结构");
        try {
            List<Menu> menuTree = menuService.findTree();
            return Result.success(menuTree);
        } catch (Exception e) {
            log.error("查询菜单树失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/type/{type}")
    public Result<List<Menu>> listByType(@PathVariable Integer type) {
        log.info("根据类型查询菜单: type={}", type);
        try {
            List<Menu> menus = menuService.findByType(type);
            return Result.success(menus);
        } catch (Exception e) {
            log.error("根据类型查询菜单失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/user/{userId}/tree")
    public Result<List<Menu>> getUserMenuTree(@PathVariable Long userId) {
        log.info("查询用户菜单树: userId={}", userId);
        try {
            List<Menu> menuTree = menuService.findUserMenuTree(userId);
            return Result.success(menuTree);
        } catch (Exception e) {
            log.error("查询用户菜单树失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/user/{userId}/perms")
    public Result<List<String>> getUserPerms(@PathVariable Long userId) {
        log.info("查询用户权限: userId={}", userId);
        try {
            List<String> perms = menuService.findUserPerms(userId);
            return Result.success(perms);
        } catch (Exception e) {
            log.error("查询用户权限失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    public Result<String> save(@RequestBody Menu menu) {
        log.info("新增菜单: {}", menu.getName());
        try {
            menuService.save(menu);
            return Result.success("新增菜单成功");
        } catch (Exception e) {
            log.error("新增菜单失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    public Result<String> update(@RequestBody Menu menu) {
        log.info("更新菜单: {}", menu.getId());
        try {
            menuService.update(menu);
            return Result.success("更新菜单成功");
        } catch (Exception e) {
            log.error("更新菜单失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除菜单: {}", id);
        try {
            menuService.deleteById(id);
            return Result.success("删除菜单成功");
        } catch (Exception e) {
            log.error("删除菜单失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}